package com.ljtxxhwj.myblob.controller;


import com.ljtxxhwj.myblob.entity.*;
import com.ljtxxhwj.myblob.service.ArticleTagService;
import com.ljtxxhwj.myblob.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljtxxhwj
 * @since 2024-07-19
 */
@RestController
@RequestMapping("/articleTag")
public class ArticleTagController {
    @Autowired
    private ArticleTagService articleTagService;

    @RequestMapping("/findAllArticleTag.do")
    @ResponseBody
    public List<ArticleTag> findAllArticleTag(){
        return articleTagService.findAllArticleTag();
    }

    @RequestMapping("/findByTagName.do")
    @ResponseBody
    public List<ArticleTag> findByTagName(String tagName){
        return articleTagService.findByTagName(tagName);
    }

    @RequestMapping("/deleteByArticleTagId.do")
    @ResponseBody
    public DialogResult deleteByArticleTagId(Integer articleTagId){
        DialogResult result = new DialogResult();
        try{
            articleTagService.deleteByArticleTagId(articleTagId);
            result.setMsg("删除成功");
        }catch (Exception e){
            result.setMsg("删除失败");
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/saveTag.do")
    @ResponseBody
    public DialogResult saveTag(ArticleTag articleTagName){
        DialogResult result = new DialogResult();
        try{
            articleTagService.saveTag(articleTagName);
            result.setMsg("新增成功");
        }catch (Exception e){
            result.setMsg("新增失败");
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/deleteBatch.do")
    @ResponseBody
    public DialogResult deleteBatch(String articleTagIds, HttpServletRequest request){
        DialogResult result = new DialogResult();
        String[] ids = articleTagIds.split(",");
        try {
            int num = articleTagService.deleteBatch(ids);
            if(num > 0){
                result.setMsg("删除成功");
            }else {
                result.setMsg("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("删除失败");
        }
        return result;
    }
}
